package com.carleasoft.mps.cms.business.write.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.cms.business.leave.bean.vo.UserListVo;
import com.carleasoft.mps.cms.business.leave.dao.LeaveMapper;
import com.carleasoft.mps.cms.business.write.bean.entity.LeadingAppointEntity;
import com.carleasoft.mps.cms.business.write.bean.po.AppointPo;
import com.carleasoft.mps.cms.business.write.dao.LeadingAppointMapper;
import com.carleasoft.mps.cms.business.write.service.LeadingAppointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author: yangHao
 * @createDate:2023/6/15
 * @description:
 */
@Service
public class LeadingAppointServiceImpl extends ServiceImpl<LeadingAppointMapper, LeadingAppointEntity> implements LeadingAppointService {
    @Autowired
    private LeaveMapper leaveMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean appoint(AppointPo appointPo) {
        List<String> userCodes = appointPo.getUserCodes();
        List<UserListVo> userListVos = leaveMapper.userInfo(userCodes);
        Map<String, UserListVo> map = userListVos.stream().collect(Collectors.toMap(UserListVo::getUserCode, Function.identity()));
        List<LeadingAppointEntity> leadingAppointEntities = new ArrayList<>();
        for (String userCode : appointPo.getUserCodes()) {
            LeadingAppointEntity leadingAppointEntity = new LeadingAppointEntity();
            leadingAppointEntity.setWriteCode(appointPo.getWriteCode());
            leadingAppointEntity.setUserCode(userCode);
            leadingAppointEntity.setUserName(map.get(userCode).getUserName());
            leadingAppointEntities.add(leadingAppointEntity);
        }
        return this.saveBatch(leadingAppointEntities);
    }
}
